import { connect } from 'dva';
import { Button } from 'antd';
import BugsList from '../components/BugsList';
import style from './bugs.css'
import BugsModal from './bugsModal'
import { dicListByPcode } from '../utils/dictionary'

const Bugs = ({dispatch, list: dataSource, arr: typeArr, loading, count, values }) => {

  function createHandler(values) {
    dispatch({
      type: 'bugs/create',
      payload: values,
    });
  }

  function fetchList(type){
    dispatch({
      type: 'bugs/fetch',
      payload: {
        search: type,
        page: 1
      }
    })
  }

  return (
    <div>
      <div className={style.create}>
        <BugsModal record={{}} onOk={createHandler}>
          <Button type="primary">新建</Button>
        </BugsModal>
      </div>
      <div className={style.filter}>
        <Button onClick={() => fetchList()}>全部</Button>
        {
          typeArr.map((item, index) => {
            return <Button key={index} onClick={() => fetchList(item.code)}>{item.name}</Button>
          })
        }
      </div>
      <BugsList dispatch={dispatch} list={dataSource} loading={loading} count={count} values={values} />
    </div>
  );
};


function mapStateToProps(state) {
  const { list, count, values } = state.bugs;
  const arr = dicListByPcode('ORDER_TYPE');
  return {
    list,
    arr,
    count,
    values,
    loading: state.loading.models.bugs,
  };
}

export default connect(mapStateToProps)(Bugs);
